/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Admin;

/**
 *
 * @author koag
 */
import supermarket.*;
import model.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class StoresPanel extends javax.swing.JPanel {

    DBmanager db;
    private int listSelectedIndex;
    private List<Store> stores;
    private DefaultListModel model;

    /**
     * Creates new form StoresPanel
     */
    public StoresPanel(DBmanager db) {
        this.db = db;
        initComponents();
        updateStoreList();
    }

    // φτιάνει το string για κάθε κατάστημα στην λίστα 
    private String toString(Store s) {
        String r = s.getName() + ",  " + s.getAddress();
        return r;
    }

    // Ενημερώνει την λίστα
    private void updateStoreList() {
        // Αρχικοποίησε Μεταβλητές
        this.listSelectedIndex = 0;
        this.Onoma.setText("");
        this.Dieythinsi.setText("");

        // Ζήτα την λίστα με τα καταστήματα απο την ΒΔ.
        stores = db.getStoreList();

        // Δημιούργησε DefaultListModel και συσχετισέτο με την λίστα μας
        model = new DefaultListModel();
        StoresList.setModel(model);

        // Καθάρισε Την λίστα μας
        model.removeAllElements();

        //Βάζουμε τα στοιχεία στο model άρα και στην λίστα.
        for (int i = 0; i < stores.size(); i++) {
            String storeLine = toString(stores.get(i));
            model.addElement(i + 1 + ". " + storeLine);
        }
        // καλού κακού κάντο και αυτό
        StoresList.revalidate();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        StoresList = new javax.swing.JList();
        b_create = new javax.swing.JButton();
        b_Save = new javax.swing.JButton();
        b_delete = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        Onoma = new javax.swing.JTextField();
        Dieythinsi = new javax.swing.JTextField();

        setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        setMinimumSize(new java.awt.Dimension(100, 0));
        setPreferredSize(new java.awt.Dimension(500, 300));

        jPanel1.setBackground(new java.awt.Color(248, 127, 7));
        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 24)); // NOI18N
        jLabel1.setLabelFor(this);
        jLabel1.setText("Διαχείριση Καταστημάτων");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        StoresList.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        StoresList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                StoresListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(StoresList);

        b_create.setText("Δημιουργία");
        b_create.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_createActionPerformed(evt);
            }
        });

        b_Save.setText("Αποθήκευση");
        b_Save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_SaveActionPerformed(evt);
            }
        });

        b_delete.setText("Διαγραφή");
        b_delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_deleteActionPerformed(evt);
            }
        });

        jLabel2.setText("Ονομα Καταστήματος  :");

        jLabel3.setText("Διεύθυνση Καταστήματος  :");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(0, 13, Short.MAX_VALUE)
                                        .addComponent(b_create)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(b_Save)
                                        .addGap(18, 18, 18)
                                        .addComponent(b_delete))
                                    .addComponent(Dieythinsi)
                                    .addComponent(Onoma)))
                            .addComponent(jScrollPane1))
                        .addGap(5, 5, 5)))
                .addGap(7, 7, 7))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(Onoma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(Dieythinsi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(b_create)
                    .addComponent(b_Save)
                    .addComponent(b_delete))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void StoresListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_StoresListMouseClicked
        // βρές την επιλεγμένη γραμμή
        this.listSelectedIndex = StoresList.getSelectedIndex();
        // Ενημέρωσε τα textBoxes
        Store s = new Store();
        s = stores.get(this.listSelectedIndex);
        this.Onoma.setText(s.getName());
        this.Dieythinsi.setText(s.getAddress());
    }//GEN-LAST:event_StoresListMouseClicked

    private void b_SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_SaveActionPerformed
        if (this.listSelectedIndex == 0) {
            return;
        }
        //Βρες ποιο κατάστημα πρέπει να ενημερωθεί
        Store s = new Store();
        s = stores.get(this.listSelectedIndex);

        // Ενημέρωσε τα πεδία του με τις αλλαγές του χρήστη
        s.setName(this.Onoma.getText());
        s.setAddress(this.Dieythinsi.getText());

        // Και στείλτο για ενημέρωση στην ΒΔ.
        int result = db.updateStore(s);

        // Ελεγξε αν ήταν επιτυχής η ενημέρωση
        // και ενημέρωσε χρήστη
        if (result == 1) {
            JOptionPane.showMessageDialog(this, SuperMarket.msgSuccModification, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(this, SuperMarket.msgUNSuccModification, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
        }

        // Επαναεισήγαγε την λίστα των Καταστημάτων
        updateStoreList();

    }//GEN-LAST:event_b_SaveActionPerformed

    private void b_deleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_deleteActionPerformed
        if (this.listSelectedIndex == 0) {
            return;
        }
        //Βρες ποιο κατάστημα πρέπει να Διαγραφεί
        Store s = new Store();
        s = stores.get(this.listSelectedIndex);
        // Και στείλτο για Διαγραφή στην ΒΔ.
        int result = db.deleteStore(s);
        // Ελεγξε αν ήταν επιτυχής η Διαγραφή
        // και ενημέρωσε χρήστη
        if (result == 1) {
            JOptionPane.showMessageDialog(this, SuperMarket.msgSuccDeletion, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(this, SuperMarket.msgUNSuccDeletion, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
        }

        // Επαναεισήγαγε την λίστα των Καταστημάτων
        updateStoreList();

    }//GEN-LAST:event_b_deleteActionPerformed

    private void b_createActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_createActionPerformed

        if (this.Onoma.getText() == "" || this.Dieythinsi.getText() == "") {
            return;
        }
        //Φτιάξε ένα κατάστημα
        Store s = new Store();

        // Ενημέρωσε τα πεδία του με τις αλλαγές του χρήστη
        s.setName(this.Onoma.getText());
        s.setAddress(this.Dieythinsi.getText());

        // Και στείλτο για ενημέρωση στην ΒΔ.
        int result = db.insertStore(s);

        // Ελεγξε αν ήταν επιτυχής η ενημέρωση
        // και ενημέρωσε χρήστη
        if (result == 1) {
            JOptionPane.showMessageDialog(this, SuperMarket.msgSuccModification, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(this, SuperMarket.msgUNSuccModification, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
        }

        // Επαναεισήγαγε την λίστα των Καταστημάτων
        updateStoreList();

    }//GEN-LAST:event_b_createActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField Dieythinsi;
    private javax.swing.JTextField Onoma;
    private javax.swing.JList StoresList;
    private javax.swing.JButton b_Save;
    private javax.swing.JButton b_create;
    private javax.swing.JButton b_delete;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
